package cn.ffcs.uec.organization.component;

import lombok.Getter;
import lombok.Setter;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zul.Div;

import cn.ffcs.uec.common.manager.CustomFilesManager;
import cn.ffcs.uec.common.model.CustomFiles;
import cn.ffcs.uec.organization.component.bean.UecDeptInfoExtBean;
import cn.ffcs.uec.organization.constant.UecOrganizationConstant;
import cn.ffcs.uec.organization.manager.UecOrgDeptManager;
import cn.ffcs.uec.organization.manager.UecOrgOrgsManager;
import cn.ffcs.uec.organization.model.UecOrgDept;
import cn.ffcs.uec.organization.model.UecOrgOrgs;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.PubUtil;

/**
 * @Title:用于点击内部树后显示节点的信息
 * @Description:将组织（单位）与部门信息放在一起，根据OrgType来分别展示
 * @Copyright: FFCS（C） 2017
 * @Company: FFCS
 * @author Wang Yiren
 * @Version 0.0.1 date:2017年12月13日
 * @since JDK1.6
 */
@Controller
@Scope("prototype")
public class UecDeptInfoExt extends Div implements IdSpace {

	private static final long serialVersionUID = -1029635625834411797L;
	
	private Logger logger = Logger.getLogger(this.getClass());

	private UecDeptInfoExtBean bean = new UecDeptInfoExtBean();
	
	private CustomFilesManager customFilesManager = (CustomFilesManager) ApplicationContextUtil
			.getBean("customFilesManager");
	
	private UecOrgDeptManager uecOrgDeptManager = (UecOrgDeptManager) ApplicationContextUtil
			.getBean("uecOrgDeptManager");
	
	private UecOrgOrgsManager uecOrgOrgsManager = (UecOrgOrgsManager) ApplicationContextUtil
			.getBean("uecOrgOrgsManager");

	private UecOrgDept uecOrgDept;

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	public UecDeptInfoExt() {
		Executions.createComponents(
				UecOrganizationConstant.UEC_DEPT_INFO_EXT_ZUL, this, null);
		Components.wireVariables(this, bean);
		Components.addForwards(this, this, '$');

		this.addForward(
				UecOrganizationConstant.ON_SELECT_UECORGDEPT_TREE_REQUEST,
				this, "onSelectUecOrgDeptTreeResponse");
		this.addForward(
				UecOrganizationConstant.ON_CLEAN_SELECT_UECORGDEPT_TREE_REQUEST,
				this, "onCleanSelectUecOrgDeptTreeResponse");
	}
	/**
	 * 初始化
	 */
	public void onCreate() {

	}
	/**
	 * 事件监听
	 * @param event
	 */
	public void onSelectUecOrgDeptTreeResponse(ForwardEvent event) {
		uecOrgDept = (UecOrgDept) event.getOrigin().getData();
		if (null != uecOrgDept) {
			logger.info("部门："+uecOrgDept.getName());
			PubUtil.fillBeanFromPo(uecOrgDept, bean);
			this.groupDataTranslationDisplay();
		}
	}
	
	public void onCleanSelectUecOrgDeptTreeResponse(ForwardEvent event){
		uecOrgDept = null;
	}
	
	/**
	 * 集团字段翻译
	 */
	public void groupDataTranslationDisplay(){
		CustomFiles customFiles = new CustomFiles();
		if(null != uecOrgDept){
			//上级部门
			if(null != uecOrgDept.getPkFatherorg()){
				UecOrgDept objUecDept = UecOrgDept.newInstance();
				objUecDept.setPkOrg(uecOrgDept.getPkFatherorg());
				UecOrgDept findUecOrgOrgsByUecOrgDept = uecOrgDeptManager.findUecOrgOrgsByUecOrgDept(objUecDept);
				if(null != findUecOrgOrgsByUecOrgDept){
					bean.getPkFatherorg().setValue(findUecOrgOrgsByUecOrgDept.getName());
				}
			}
			//部门类型
			if(null != uecOrgDept.getDepttype()){
				Long depttype = uecOrgDept.getDepttype();
				if(0 == depttype){
					bean.getDepttype().setValue("内部部门");
				}else if(1 == depttype){
					bean.getDepttype().setValue("外部第三方部门");
				}
			}
			//部门类型
			if(null != uecOrgDept.getGlbdef2()){
				customFiles.setPkDefdoc(uecOrgDept.getGlbdef2());
				CustomFiles findCustomFilesByCustomFiles = customFilesManager.findCustomFilesByCustomFiles(customFiles);
				if(null != findCustomFilesByCustomFiles){
					bean.getGlbdef2().setValue(findCustomFilesByCustomFiles.getName());
				}
			}
			
			//HR撤销标志
			//机构类型
			if(null != uecOrgDept.getGlbdef3()){
				customFiles.setPkDefdoc(uecOrgDept.getGlbdef3());
				CustomFiles findCustomFilesByCustomFiles = customFilesManager.findCustomFilesByCustomFiles(customFiles);
				if(null != findCustomFilesByCustomFiles){
					bean.getGlbdef3().setValue(findCustomFilesByCustomFiles.getName());
				}
			}
			//渠道类型
			if(null != uecOrgDept.getGlbdef7()){
				customFiles.setPkDefdoc(uecOrgDept.getGlbdef7());
				CustomFiles findCustomFilesByCustomFiles = customFilesManager.findCustomFilesByCustomFiles(customFiles);
				if(null != findCustomFilesByCustomFiles){
					bean.getGlbdef7().setValue(findCustomFilesByCustomFiles.getName());
				}
			}
			//是否是承包单元
			if(null != uecOrgDept.getGlbdef10()){
				String glbdef10 = uecOrgDept.getGlbdef10();
				if("Y".equals(glbdef10)){
					bean.getGlbdef10().setValue("是");
				}else if("N".equals(glbdef10)){
					bean.getGlbdef10().setValue("否");
				}
			}
			//服务区域
			if(null != uecOrgDept.getGlbdef11()){
				customFiles.setPkDefdoc(uecOrgDept.getGlbdef11());
				CustomFiles findCustomFilesByCustomFiles = customFilesManager.findCustomFilesByCustomFiles(customFiles);
				if(null != findCustomFilesByCustomFiles){
					bean.getGlbdef11().setValue(findCustomFilesByCustomFiles.getName());
				}
			}
			//单位属性
			if(null != uecOrgDept.getGlbdef14()){
				customFiles.setPkDefdoc(uecOrgDept.getGlbdef14());
				CustomFiles findCustomFilesByCustomFiles = customFilesManager.findCustomFilesByCustomFiles(customFiles);
				if(null != findCustomFilesByCustomFiles){
					bean.getGlbdef14().setValue(findCustomFilesByCustomFiles.getName());
				}
			}
			
			if(null != uecOrgDept.getGlbdef15()){
				customFiles.setPkDefdoc(uecOrgDept.getGlbdef15());
				CustomFiles findCustomFilesByCustomFiles = customFilesManager.findCustomFilesByCustomFiles(customFiles);
				if(null != findCustomFilesByCustomFiles){
					bean.getGlbdef15().setValue(findCustomFilesByCustomFiles.getName());
				}
			}
			
			if(null != uecOrgDept.getGlbdef16()){
				customFiles.setPkDefdoc(uecOrgDept.getGlbdef16());
				CustomFiles findCustomFilesByCustomFiles = customFilesManager.findCustomFilesByCustomFiles(customFiles);
				if(null != findCustomFilesByCustomFiles){
					bean.getGlbdef16().setValue(findCustomFilesByCustomFiles.getName());
				}
			}
			//所属业务单元
			if(null != uecOrgDept.getPkOrg()){
				UecOrgOrgs uecOrgOrgs = new UecOrgOrgs();
				uecOrgOrgs.setPkOrg(uecOrgDept.getPkOrg());
				UecOrgOrgs findUecOrgOrgsByUecOrgOrgs = uecOrgOrgsManager.findUecOrgOrgsByUecOrgOrgs(uecOrgOrgs);
				if(null != findUecOrgOrgsByUecOrgOrgs){
					bean.getPkOrg().setValue(findUecOrgOrgsByUecOrgOrgs.getName());
				}
			}
			//部门级别
			if(null != uecOrgDept.getDeptlevel()){
				customFiles.setPkDefdoc(uecOrgDept.getDeptlevel());
				CustomFiles findCustomFilesByCustomFiles = customFilesManager.findCustomFilesByCustomFiles(customFiles);
				if(null != findCustomFilesByCustomFiles){
					bean.getDeptlevel().setValue(findCustomFilesByCustomFiles.getName());
				}
			}
			
		}
	}
	

}
